<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生作业提交系统 - 登录测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 500px;
            margin: 0 auto;
            padding: 20px;
            box-sizing: border-box;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select, button {
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f0f0f0;
            border-radius: 5px;
        }
        .error {
            color: red;
        }
        .success {
            color: green;
        }
    </style>
</head>
<body>
    <h1>学生作业提交系统 - 登录测试</h1>
    
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" required>
        </div>
        
        <div class="form-group">
            <label for="password">密码：</label>
            <input type="password" id="password" name="password" required>
        </div>
        
        <div class="form-group">
            <label for="role">角色：</label>
            <select id="role" name="role" required>
                <option value="student">学生</option>
                <option value="teacher">教师</option>
            </select>
        </div>
        
        <div class="form-group">
            <label for="apiUrl">API地址：</label>
            <input type="text" id="apiUrl" name="apiUrl" value="http://127.0.0.1:3000" required>
        </div>
        
        <button type="submit">登录</button>
    </form>
    
    <div id="result" class="result">
        <p>登录结果将显示在这里</p>
    </div>
    
    <script>
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const role = document.getElementById('role').value;
            const apiUrl = document.getElementById('apiUrl').value;
            const resultDiv = document.getElementById('result');
            
            resultDiv.innerHTML = '<p>正在登录...</p>';
            
            try {
                const response = await fetch(`${apiUrl}/api/users/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username, password, role })
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP错误! 状态: ${response.status}`);
                }
                
                const data = await response.json();
                
                if (data.success) {
                    // 登录成功
                    resultDiv.innerHTML = `
                        <p class="success">登录成功！</p>
                        <p>用户信息: ${data.user.username}</p>
                        <p>角色: ${data.user.role}</p>
                        <p>Token: ${data.token}</p>
                    `;
                    // 保存token和API地址到localStorage
                    localStorage.setItem('token', data.token);
                    localStorage.setItem('apiBaseUrl', apiUrl);
                    
                    // 跳转到主应用
                    setTimeout(() => {
                        window.location.href = 'http://127.0.0.1:8080/';
                    }, 2000);
                } else {
                    // 登录失败
                    resultDiv.innerHTML = `<p class="error">登录失败: ${data.message || '未知错误'}</p>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<p class="error">请求错误: ${error.message}</p>`;
                console.error('登录请求错误:', error);
            }
        });
        
        // 测试WebSocket连接
        function testWebSocketConnection() {
            const wsResult = document.createElement('div');
            wsResult.innerHTML = '<p>测试WebSocket连接...</p>';
            document.body.appendChild(wsResult);
            
            try {
                // 创建WebSocket连接
                const ws = new WebSocket('ws://localhost:8080/');
                
                ws.onopen = function() {
                    wsResult.innerHTML = '<p class="success">WebSocket连接成功！</p>';
                    ws.close();
                };
                
                ws.onerror = function(error) {
                    wsResult.innerHTML = `<p class="error">WebSocket连接失败: ${error.message || '无法连接'}</p>`;
                };
                
                // 5秒后如果没有成功连接，视为失败
                setTimeout(() => {
                    if (ws.readyState !== WebSocket.OPEN) {
                        wsResult.innerHTML = '<p class="error">WebSocket连接超时</p>';
                        ws.close();
                    }
                }, 5000);
            } catch (error) {
                wsResult.innerHTML = `<p class="error">WebSocket错误: ${error.message}</p>`;
            }
        }
        
        // 页面加载后测试WebSocket连接
        window.addEventListener('load', testWebSocketConnection);
    </script>
</body>
</html>